package com.zj.demo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zj.demo.entity.Section;

@WebServlet("/validaccount")
public class ValidAccountServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到用户提交过来的账号
		String account = request.getParameter("account");
		//设置相应的编码
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		//得到输出对象
		PrintWriter out = response.getWriter();
		
//		if(account.equals("admin")){
//			System.out.println("账号已存在");
////			out.print(false);
//			out.print("{\"success\":false}"); //json对象属性带引号
//		}else{
//			System.out.println("账号可用");
////			out.print(true);
//			out.print("{\"success\":true}");
//		}
		
		Map<String, Object> result = new HashMap<>();
		if(account.equals("admin")){
			result.put("success", false);
		}else{
			result.put("success", true);
		}
		
		List<Section> sections = new ArrayList<>();
		sections.add(new Section(1,"官方公告", null, null, null));
		sections.add(new Section(2,"官方公告1", null, null, null));
		sections.add(new Section(3,"官方公告2", null, null, null));
		sections.add(new Section(4,"官方公告3", null, null, null));
		sections.add(new Section(5,"官方公告4", null, null, null));
		
		result.put("data", sections);
		
		ObjectMapper objectMapper = new ObjectMapper();
		String jsonString = objectMapper.writeValueAsString(result);
		System.out.println(jsonString);
		out.println(jsonString);
	}

	
}
